﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using System.Configuration;

namespace MPP.Product.DataManage
{
    public class SqlHelper
    {
        /// <summary>
        /// Gets the oracle main DB connection string.
        /// </summary>
        /// <remarks></remarks>
        public static string OracleMainDBConnectionString
        {
            get { return ConfigurationManager.ConnectionStrings["oracleMainDB"].ConnectionString; }
        }

        /// <summary>
        /// Gets the next ID.
        /// </summary>
        /// <param name="tableName">Name of the table.</param>
        /// <param name="idColumnName">Name of the id column.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static int GetNextID(string tableName, string idColumnName)
        {

            string strConn = SqlHelper.OracleMainDBConnectionString;
            int result = 0;
            using (OracleConnection oracleConn = new OracleConnection(strConn))
            {
                oracleConn.Open();

                StringBuilder strSql = new StringBuilder();

                strSql.Append("Select  max(" + idColumnName + ")  from " + tableName);

                OracleCommand oracleCommand = new OracleCommand(strSql.ToString(), oracleConn);
                OracleDataAdapter adapter = new OracleDataAdapter(strSql.ToString(), oracleConn);
                DataTable datatable = new DataTable();
                adapter.Fill(datatable);
                oracleConn.Close();
                            
                 if (Convert.ToInt32(datatable.Rows[0][0]) >0)
                    {
                        result = Convert.ToInt32(datatable.Rows[0][0]);
                    }
                }             
               return result+1;
        }

    }
}
